// pages/hotel/order.js
import {
    hotel,
    user,
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [{
            path: '/images/banner.png',
            url: ''
        }],
        obj: '',
        contact_name: '',
        contact_phone: '',
        note: '',
        order_id: '',
        order_no: '',
        room_quantity: 1,
        listCou: [],
        radio: '',
        couItem: "",
        coupon_id:'',
        total_price:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let obj = JSON.parse(options.obj)
        this.setData({
            obj: obj
        })
        this.getDetail()
        this.getList()

    },
    onClick(event) {
        console.log(event)
        const {
            id,
            item,
            index
        } = event.currentTarget.dataset;
        this.setData({
            radio: id,
            coupon_id: id,
            couItem: item,
            couIndex:index,
            total_price:this.data.info.total_price*this.data.room_quantity-Number(item.amount)
        });
        // this.toConOrder()
    },
    toCancelCou(){
        this.setData({
            radio:'',
            coupon_id:'',
            couItem:'',
            couIndex:'',
            showCoupon: false
        })
    },
    onChangeCou(e){
        // this.setData({
        //     radio: e.detail,
        // });
    },
    toComCou(){
        this.setData({
            showCoupon: false
        })
    },
    toShowCou() {
        this.setData({
            showCoupon: true
        })
    },
    onCloseCou() {
        this.setData({
            showCoupon: false
        })
    },
    getList() {
        user.coupon.usableList({
            use_scene: 2,
            hotel_room_type_id: this.data.obj.type_id,
            quantity: this.data.room_quantity,
            checkin_date: this.data.obj.checkin_date,
            checkout_date: this.data.obj.checkout_date
        }, res => {
            if (res.code == 1) {
                this.setData({
                    listCou: res.data
                })
                if(res.data.length>0){
                    this.setData({
                        couItem:res.data[0],
                        radio:res.data[0].id,
                        coupon_id:res.data[0].id,
                        total_price:Number(this.data.info.total_price*this.data.room_quantity)-Number(res.data[0].amount)
                    })
                }
                // if(this.data.couItem!=''){
                //     if(res.data[this.data.couIndex]){
                //         this.setData({
                //             total_price:this.data.info.total_price*this.data.room_quantity-Number(res.data[this.data.couIndex].amount)
                //         })
                //     }else{
                //         this.setData({
                //             couItem:''
                //         })
                //     }
                //   }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toPre(e) {
        let imgs = e.currentTarget.dataset.imgs
        let index = e.currentTarget.dataset.index
        wx.previewImage({
            urls: imgs,
            current: index
        })
    },
    inputChange(e) {
        let fieldName = e.target.dataset.name
        this.setData({
            [`${fieldName}`]: e.detail.value
        })
    },
    onChange(e) {
        console.log(e.detail)
        this.setData({
            room_quantity: e.detail
        })
        this.getList()
    },
    // 预定
    toOrder() {
        let obj = this.data.obj
        obj.contact_name = this.data.contact_name
        obj.contact_phone = this.data.contact_phone
        obj.note = this.data.note
        obj.room_quantity = this.data.room_quantity
        obj.user_coupon_id = this.data.coupon_id
        console.log(obj)
        if (obj.contact_name == '') {
            wx.showToast({
                title: '请输入住客姓名',
                icon: 'error'
            })
        }
        if (obj.contact_phone == '') {
            wx.showToast({
                title: '请输入联系电话',
                icon: 'error'
            })
        }
        let that = this
        hotel.order.submit(obj, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg,
                })
                that.setData({
                    order_id: res.data.id,
                    order_no: res.data.order_no,
                })
                that.toPay()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })

    },
    // 支付
    toPay() {
        // hotel.order.pay({

        // })
        let order_no = this.data.order_no
        wx.login({
            success(res) {
                var code = res.code
                wx.showLoading({
                    title: "请稍后...",
                    mask: true
                })
                hotel.order.pay({
                    order_no,
                    code
                }, res => {
                    let ret_code = res.code
                    let ret_msg = res.msg
                    if (ret_code != 1) {
                        //   Toast(ret_msg)
                        wx.showToast({
                            title: ret_msg,
                            icon: 'error'
                        })
                        return
                    }
                    let {
                        nonceStr,
                        paySign,
                        signType,
                        timeStamp
                    } = res.data
                    wx.requestPayment({
                        nonceStr,
                        package: res.data.package,
                        paySign,
                        timeStamp,
                        signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1500
                            });
                            setTimeout(() => {
                                wx.redirectTo({
                                  url: '/pages/order/index',
                                })
                            }, 1000)
                        },
                        fail(res) {
                            wx.showToast({
                                title: '支付失败',
                                duration: 1500,
                                icon: 'error'
                            });
                            setTimeout(() => {
                                wx.redirectTo({
                                    url: '/pages/order/index',
                                  })
                            }, 1000)

                        }
                    })
                })
            }
        })
    },
    getDetail() {
        hotel.room.detail({
            type_id: this.data.obj.type_id,
            checkin_date: this.data.obj.checkin_date,
            checkout_date: this.data.obj.checkout_date
        }, res => {
            if (res.code == 1) {
                this.setData({
                    info: res.data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})